#include "OptionsWindow.h"

OptionsWindow::OptionsWindow(QWidget *parent) : QDialog(parent){
    setupUi(this);
    this->_loadAvailablesLangages();
    this->loadSettings();

    //01 : interface
    this->connect(this->langagesComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(setNewLangage(QString)));
    this->connect(this->continuePlayingCheckBox, SIGNAL(toggled(bool)), this, SLOT(setIsContinuePlayling(bool)));
    this->connect(this->extractionComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setIdExtractionMethod(int)));
    // 01.1 method 01
    this->connect(this->maxBeforeSecSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setAutomaticMaxNSecBefore(int)));
    this->connect(this->maxAfterSecSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setAutomaticMaxNSecAfter(int)));
    this->connect(this->sensibilitySpinBox, SIGNAL(valueChanged(int)), this, SLOT(setAutomaticSensibility(int)));
    this->connect(this->medianDataSizeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setAutomaticMedianDataSize(int)));
    // 01.1 method 02
    this->connect(this->beforeSecSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setNormalNSecBefore(int)));
    this->connect(this->afterSecSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setNormalNSecAfter(int)));
    //02 : subtitle
    //03 : video
    //04 : audio
    this->connect(this->volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(setDefaultVolume(int)));
}

OptionsWindow::~OptionsWindow(){
}

void OptionsWindow::_loadAvailablesLangages(){
    QDirIterator dirIterator(":/langages");
    this->langagesComboBox->addItem("fr");
    while(dirIterator.hasNext()){
        QString langageName(dirIterator.next());
        if(langageName.endsWith("qm")){
            langageName = langageName.right(5).left(2); //TODO set a more beautiful langage name
            this->langagesComboBox->addItem(langageName);
        }
    }
}

void OptionsWindow::loadSettings(){
    //TODO what append the first time the application is launch ? May be we can do a test : if it's the first time we first set defaut setting values
    //01 : interface
    this->langagesComboBox->setCurrentIndex(this->settings.getIdLangage());
    this->continuePlayingCheckBox->setChecked(this->settings.isContinuePlaying());
    this->extractionComboBox->setCurrentIndex(this->settings.getIdExtractionMethod());

    // 01.1 method 01
    this->maxBeforeSecSpinBox->setValue(this->settings.getAutomaticMaxNSecBefore());
    this->maxAfterSecSpinBox->setValue(this->settings.getAutomaticMaxNSecAfter());
    this->sensibilitySpinBox->setValue(this->settings.getAutomaticSensibility());
    this->medianDataSizeSpinBox->setValue(this->settings.getAutomaticMedianDataSize());
    // 01.1 method 02
    this->beforeSecSpinBox->setValue(this->settings.getNormalNSecBefore());
    this->afterSecSpinBox->setValue(this->settings.getNormalNSecAfter());
    //02 : subtitle
    //03 : video
    //04 : audio
    this->volumeSlider->setValue(this->settings.getVolume());
}

void OptionsWindow::setNewLangage(QString langage){
    if (langage.compare(settings.getLangage()) != 0){
        this->settings.setLangage(langage);
        this->settings.setIdLangage(this->langagesComboBox->currentIndex());
        QMessageBox::information(this, tr("Changement de la langue"), tr("La langue a bien été modifiée. Le changement sera pris en compte au prochain démarage de l'application."));
        //TODO find out how to translate that
    }
}


//01 : interface
void OptionsWindow::setDefaultVolume(int percentage){
    this->settings.setVolume(percentage);
}

void OptionsWindow::setIsContinuePlayling(bool isContinuePlaying){
    this->settings.setContinuePlaying(isContinuePlaying);
}

void OptionsWindow::setIdExtractionMethod(int id){
    this->settings.setIdExtractionMethod(id);
}
// 01.1 method 01
void OptionsWindow::setAutomaticMaxNSecBefore(int nSecBefore){
    this->settings.setAutomaticMaxNSecBefore(nSecBefore);
}

void OptionsWindow::setAutomaticMaxNSecAfter(int nSecAfter){
    this->settings.setAutomaticMaxNSecAfter(nSecAfter);
}

void OptionsWindow::setAutomaticSensibility(int sensibility){
    this->settings.setAutomaticSensibility(sensibility);
}

void OptionsWindow::setAutomaticMedianDataSize(int medianDataSize){
    this->settings.setAutomaticMedianDataSize(medianDataSize);
}
// 01.1 method 02
void OptionsWindow::setNormalNSecBefore(int nSecBefore){
    this->settings.setNormalNSecBefore(nSecBefore);
}

void OptionsWindow::setNormalNSecAfter(int nSecAfter){
    this->settings.setNormalNSecAfter(nSecAfter);
}

//02 : subtitle
//03 : video
//04 : audio


